package com.maya.medicalwastes.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.maya.medicalwastes.R;
import com.maya.medicalwastes.util.DeviceUtil;


/**
 * @description 加载框
 * @user gzl
 * @date 2017/4/21
 * @time 17:26
 */

public class LoadingDialog extends Dialog {
    private static LoadingDialog mLoadingDialog;

    public LoadingDialog(Context context) {
        super(context);
    }

    public LoadingDialog(Context context, int theme) {
        super(context, theme);
    }

    protected LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static LoadingDialog show(Context context, String message, boolean cancelable) {
        if (context != null) {
            mLoadingDialog = new LoadingDialog(context,
                    R.style.AlertDialog);
            mLoadingDialog.setContentView(R.layout.dialog_loading);
            mLoadingDialog.setCancelable(cancelable);
            mLoadingDialog.setOnKeyListener(onKeyListener);
            mLoadingDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
            mLoadingDialog.getWindow().getAttributes().height = DeviceUtil.getDisplayMetrics(context).heightPixels; // 改变的是dialog框在屏幕中的位置而不是大小
            mLoadingDialog.getWindow().getAttributes().width = DeviceUtil.getDisplayMetrics(context).widthPixels; // 改变的是dialog框在屏幕中的位置而不是大小
            TextView tv_message = (TextView) mLoadingDialog
                    .findViewById(R.id.tv_msg);
            if (null == message || "".equals(message))
                tv_message.setVisibility(View.GONE);
            else
                tv_message.setText(message);
            mLoadingDialog.show();
        } else {
            mLoadingDialog = null;
        }
        return mLoadingDialog;
    }

    public static LoadingDialog show(Context context, String message) {
        return show(context, message, false);

    }

    public static LoadingDialog show(Context context, boolean cancelable) {
        return show(context, "", cancelable);

    }

    public static LoadingDialog show(Context context) {
        return show(context, "", false);
    }

    @Override
    public void cancel() {
        // TODO Auto-generated method stub
        super.cancel();
        mLoadingDialog = null;
    }

    @Override
    public void dismiss() {
        // TODO Auto-generated method stub
        super.dismiss();
        mLoadingDialog = null;
    }

    private static OnKeyListener onKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
//                if (mLoadingDialog != null && mLoadingDialog.isShowing())
//                    mLoadingDialog.dismiss();
            }
            return false;
        }
    };
}
